In [1]:
import numpy as np
from sklearn.datasets import make_regression
from scipy.spatial.distance import norm
from itertools import product
from collections import OrderedDict
from plotly.graph_objs import *
import plotly.tools as tls
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode()
import time

from plot_helpers import *
In [2]:
X, y = make_regression(n_samples = int(1e5), n_features = 2, n_informative=2, random_state=0, noise=10)
X = (X - X.mean(axis=0))/X.std()
In [3]:
# LINEAR REGRESSION WITHOUT REGULARIZATION

def ols_cost_function(X, y, params):
    '''
    OLS from linear regression
    '''
    n_observations = X.shape[0]
    avg_squared_residuals = ((predict(X, params) - y)**2).sum()/(2*n_observations)
    return avg_squared_residuals

def ols_gradient_of_cost_function(X, y, params):
    n_observations = X.shape[0]
    gradient = (predict(X, params) - y).dot(X)/n_observations
    return gradient

gd_param_history, gd_time_history = gradient_descent(X, y, ols_cost_function, ols_gradient_of_cost_function,
                initial_guess = np.array([0., 0.]))

figure_3d = plot_results(X, y, ols_cost_function, gd_param_history)
iplot(figure_3d)
Final gradient of cost function [-0.00079784 -0.00043608]
Final params [ 98.08097904  55.50870141]